Skip to content

Yield Overrides#47548

Closed
arcanis wants to merge 3 commits into
microsoft:mainfrom
arcanis:mael/yield-override
Closed

Yield Overrides#47548
arcanis wants to merge 3 commits into
microsoft:mainfrom
arcanis:mael/yield-override

Conversation

@arcanis

@arcanis arcanis commented Jan 21, 2022

Copy link
Copy Markdown
Contributor

This PR implements #43632 and provides a way to annotate values in such a way that, when yielded, they return a specific other type. This allows libraries built around generators to provide proper typings:

declare function select<T>(fn: () => T): YieldType<T>;

function* generator() {
  const squareValue = yield select(state => state.number ** 2);
  assertEqual<number>()(squareValue);

  const isGreaterThan10 = yield select(state => state.number > 10);
  assertEqual<boolean>()(isGreaterThan10);

  const hexValue = yield select(state => state.number.toString(16));
  assertEqual<string>()(hexValue;
}

Fixes #43632

cc @andrewbranch for the playground link

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jan 21, 2022
@andrewbranch

Copy link
Copy Markdown
Member

@typescript-bot pack this

@typescript-bot

typescript-bot commented Jan 21, 2022

Copy link
Copy Markdown
Contributor

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at 9fc3cc5. You can monitor the build here.

@arcanis

arcanis commented Jan 21, 2022

Copy link
Copy Markdown
Contributor Author

Sorry about that, tests should be fixed; can you try to re-run the pack?

@andrewbranch

Copy link
Copy Markdown
Member

@typescript-bot pack this

@typescript-bot

typescript-bot commented Jan 24, 2022

Copy link
Copy Markdown
Contributor

Heya @andrewbranch, I've started to run the tarball bundle task on this PR at c9f8bb2. You can monitor the build here.

@typescript-bot

typescript-bot commented Jan 24, 2022

Copy link
Copy Markdown
Contributor

Hey @andrewbranch, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/118156/artifacts?artifactName=tgz&fileId=49D5863517A089D4E367A13C0EC34BE731BA8B8BB714FF38756C82B1AA70226002&fileName=/typescript-4.6.0-insiders.20220124.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/pr-build@4.6.0-pr-47548-5".;

Comment thread src/lib/es5.d.ts
/**
* Marker for yield expressions return types
*/
interface YieldType<T> { }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use type YieldType<T> = intrinsic;? (see #46836 for ThisType above)

@neurosnap

Copy link
Copy Markdown

I'd love to help in anyway possible to get this across the finish line. Is there anything I can do to help?

@arcanis

arcanis commented Feb 27, 2022

Copy link
Copy Markdown
Contributor Author

It should already be ok apart from a couple of extra tests; next blocker is to find someone to champion it in the TS team.

@neurosnap

Copy link
Copy Markdown

How can we find someone to champion this PR?

@andrewbranch

Copy link
Copy Markdown
Member

To be clear, nobody on the TypeScript team ever indicated that this approach to solving this problem would be mergeable. @arcanis wrote it as an experiment purely to help the discussion in #43632 along. Given the discussion there, I personally feel that this is not the right approach—this is essentially just a type assertion, but piggy-backing on non-type-assertion syntax in such a way that nobody will recognize that it’s just as unsafe. The next step is not finding someone to champion this PR; we are still on the very first step: exploring possible solutions that could be driven to consensus.

@scorbiclife scorbiclife mentioned this pull request Sep 9, 2022
5 tasks
@sandersn

sandersn commented Apr 1, 2025

Copy link
Copy Markdown
Member

I'm closing this PR because it is stale and the original issue has open design questions. If the original issue is accepted, we can revisit the PR to see if any of the code is still usable

@sandersn sandersn closed this Apr 1, 2025
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

💡 Yield Overrides

7 participants